﻿using System;
using System.Web.Mvc;
using Kratos.Framework.ContentManagement.Models;
using Kratos.Framework.Extensions;
using Kratos.Framework.Filters;
using Kratos.Framework.Function;
using Kratos.Framework.Modules.Core.Controllers;
using Kratos.Framework.Modules.Core.Services;
using Kratos.Modules.Test.Services;

namespace Kratos.Modules.Test.Controllers
{
    [HandleError]
    public class HomeController : KratosController
    {
        private readonly ITestService _service;
        private readonly IUserInfoServices _userService;
        private readonly IActionPermissionService _apSrv;

        public HomeController(ITestService service, IUserInfoServices userService, IActionPermissionService apSrv)
        {
            _service = service;
            _userService = userService;
            _apSrv = apSrv;
        }

        [Ignore]
        public ActionResult Index()
        {
            return View();
        }

        [Ajax(HttpVerbs.Post)]
        public ActionResult Test(int? p)
        {
            //类型转换测试OK
            ViewBag.Test = "123".ToSafeInt();
            ViewBag.Date = "2012/3/5".ToSafeDateTime();
            ViewBag.DateFormat = DateTime.Now.ToDateString();
            ViewBag.S = "I Love {0}.".With("You");

            var user = _userService.CurrentUser;

            //缓存测试OK
            var s = KratosCache.Get<ActionPermission>("ss");
            if (s == null)
            {
                var d = new ActionPermission { ActionName = "jjjj", ControllerName = "ccccccc" };
                KratosCache.Set("ss", d);
            }

            return PartialView("IndexPartial", _service.GetAll(p.HasValue ? p.Value - 1 : 0));
        }

        public ActionResult SysType()
        {
            return View();
        }

        public ActionResult GetTime()
        {
            return Content(@"alert(""DateTime.Now.ToString()"")");
        }
    }
}
